//
//  DYTableViewController.m
//  Excellence
//
//  Created by 帝云科技 on 2017/6/16.
//  Copyright © 2017年 diyunkeji. All rights reserved.
//

#import "DYTableViewController.h"


@interface DYTableViewController ()

@end

@implementation DYTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)dy_initData
{
    [super dy_initData];
    _style = DYTableViewStylePlain;
}

- (void)dy_initUI
{
    [super dy_initUI];
    [self.view addSubview:self.tableView];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        UITableViewStyle style;
        switch (_style)
        {
            case DYTableViewStyleGrouped:
                style = UITableViewStyleGrouped;
                break;
            case DYTableViewStylePlain:
            default:
                style = UITableViewStylePlain;
                break;
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationStatusHeight)
                                                 style:style];
        _tableView.backgroundColor = [UIColor xhq_grayblue];
        _tableView.tableFooterView = [UIView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        @weakify(self);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            @strongify(self);
            self->_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        });
        
        [_tableView xhq_registerCell:[DYTableViewCell class]];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFLOAT_MIN)];
        if (@available(iOS 11.0, *))
        {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever ;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYTableViewCellItem *item = self.dataArray[indexPath.section][indexPath.row];
    DYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:item.cellIdentifier forIndexPath:indexPath];
    cell.item = item;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYTableViewCellItem *item = self.dataArray[indexPath.section][indexPath.row];
    if (item.cellHeight > 0)
    {
        return item.cellHeight;
    }
    else
    {
        return [item.cellClass hyb_heightForTableView:tableView config:^(UITableViewCell *sourceCell) {
            DYTableViewCell *cell = (DYTableViewCell *)sourceCell;
            cell.item = item;
        }];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return ({
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor xhq_grayblue];
        view;
    });
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return ({
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor xhq_grayblue];
        view;
    });
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}

#pragma mark - emptyDataSet
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSAttributedString *attribute = [[NSAttributedString alloc]initWithString:@"— 无数据 —"
                                                                   attributes:@{
                                                                                NSForegroundColorAttributeName: [UIColor xhq_assist],
                                                                                NSFontAttributeName: [UIFont xhq_font14]
                                                                                }];
    return attribute;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    BOOL res = self.tableView.backgroundColor == [UIColor xhq_section];
    return res ? [UIImage imageNamed:@"img_default"] : [UIImage imageNamed:@"img_wujh"];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    return [[NSAttributedString alloc]initWithString:@""
                                          attributes:@{NSForegroundColorAttributeName: [UIColor xhq_assist]}];
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return self.tableView.backgroundColor;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -100.f;
}

- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView {
    scrollView.contentOffset = CGPointZero;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

#pragma mark - configureData
- (void)configureData{}
- (void)configureDataFromModel:(DYModel *)model{}

#pragma mark - getter
- (NSMutableArray *)sectionArray0 {
    if (!_sectionArray0) {
        _sectionArray0 = [[NSMutableArray alloc]init];
    }
    return _sectionArray0;
}

- (NSMutableArray *)sectionArray1 {
    if (!_sectionArray1) {
        _sectionArray1 = [[NSMutableArray alloc]init];
    }
    return _sectionArray1;
}

- (NSMutableArray *)sectionArray2 {
    if (!_sectionArray2) {
        _sectionArray2 = [[NSMutableArray alloc]init];
    }
    return _sectionArray2;
}

- (NSMutableArray *)sectionArray3 {
    if (!_sectionArray3) {
        _sectionArray3 = [[NSMutableArray alloc]init];
    }
    return _sectionArray3;
}

- (NSMutableArray *)sectionArray4 {
    if (!_sectionArray4) {
        _sectionArray4 = [[NSMutableArray alloc]init];
    }
    return _sectionArray4;
}

@end
